package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.correction.model.enumeration.St_work;
import com.power.sifa.correction.service.Cre_warningService;
import com.power.sifa.correction.service.Cre_workService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Cre_work_repeat;
import com.power.sifa.correction.model.pageInfo.Cre_work_repeatPageInfo;
import com.power.sifa.correction.api.ICre_work_repeatFeign;
import com.power.sifa.correction.service.Cre_work_repeatService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   再办任务控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Cre_work_repeatController",tags={"再办任务"})
public class Cre_work_repeatController implements ICre_work_repeatFeign{

    @Resource
    private Cre_work_repeatService cre_work_repeatService;
    @Resource
    private Cre_warningService cre_warningService;

    @Override
    @ApiOperation("查询再办任务分页列表")
    @SaCheckPermission("cre_work_repeat_list")
    public ResponseDTO list(Cre_work_repeatPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Cre_work_repeat> page = this.cre_work_repeatService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Cre_work_repeat cre_work_repeat : page) {
                cre_work_repeat.geneText();
                list.add(cre_work_repeat);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看再办任务")
    @SaCheckPermission("cre_work_repeat_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Cre_work_repeat cre_work_repeat  = cre_work_repeatService.getById(id);
        if (cre_work_repeat != null) {
            cre_work_repeat.geneText();
        }
        return ResponseDTO.succData(cre_work_repeat);
    }

    @Override
    @ApiOperation("删除再办任务")
    @SaCheckPermission("cre_work_repeat_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        cre_work_repeatService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改再办任务")
    @SaCheckPermission("cre_work_repeat_save")
    public ResponseDTO save(Cre_work_repeat cre_work_repeat) {
        Assert.notNull(cre_work_repeat, "cre_work_repeat不能为空");
        Assert.notNull(cre_work_repeat.getWork(), "预警任务信息不能为空");
        if(null == cre_work_repeat.getCreatetime()) {
            cre_work_repeat.setCreatetime(new Date());
        }
        // 1. 保存再办记录
        this.cre_work_repeatService.saveOrUpdate(cre_work_repeat);
        // 2. 更新再办状态
        this.cre_warningService.updateIsrepeat(cre_work_repeat.getWarnid(), YesNo.YES);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除再办任务")
    @SaCheckPermission("cre_work_repeat_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        cre_work_repeatService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("cre_work_repeat_list")
    public void output(HttpServletResponse response, Cre_work_repeatPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "再办任务";
        String[] heads = {"序号","预警id","原始任务id","再办下达人id","再办下达人姓名","再办意见","下达时间"};
        String[] fields = {"id","warnid","workid","createuserid","createusername","remark","createtime"};
        Page<Cre_work_repeat> page = this.cre_work_repeatService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "再办任务";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","预警id","原始任务id","再办下达人id","再办下达人姓名","再办意见","下达时间"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","warnid","workid","createuserid","createusername","remark","createtime"};
        List<Cre_work_repeat> list = (List<Cre_work_repeat>)ExcelUtil.Import(fields, file, Cre_work_repeat.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.cre_work_repeatService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
